<!--
 * @Author: maqaing
 * @Date: 2021-10-22 14:14:51
 * @LastEditors: 会飞的蕉太狼
 * @LastEditTime: 2021-10-27 19:20:59
 * @Description: 
-->
<template>
  <div>
    <!-- 新增按钮 开始-->
    <el-form :inline="true" :model="queryData">
      <el-button type="primary" style="margin-right: 15px" @click="toAdd"
        >添加</el-button
      >
      <el-form-item>
        <el-input
          v-model="queryData.username"
          placeholder="请输入用户名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="queryData.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option label="禁用" value="禁用"></el-option>
          <el-option label="正常" value="正常"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 新增按钮 结束-->

    <!-- 员工数据 -->
    <el-table :data="tableData.list" style="width: 100%" size="mini">
      <el-table-column prop="id" label="编号" align="center"> </el-table-column>
      <el-table-column prop="username" label="用户名" align="center">
      </el-table-column>
      <el-table-column prop="realname" label="姓名" align="center">
      </el-table-column>
      <el-table-column prop="telephone" label="手机号" align="center">
      </el-table-column>
      <el-table-column label="性别" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.gender == 'male'">男</span>
          <span v-else-if="scope.row.gender=='female'">女</span>
          <span v-else>未选择</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template v-slot="scope">
          <el-tag v-if="scope.row.status == '正常'" type="success" size="mini">
            {{ scope.row.status }}
          </el-tag>
          <el-tag v-else type="danger" size="mini">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      //操作列
      <el-table-column fixed="right" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            @click="toEdit(scope.row)"
            type="text"
            size="small"
            style="color: #49b99d"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="toDelete(scope.row.id)"
            style="color: #e66272"
            >删除</el-button
          >
          <el-button
            type="text"
            size="small"
            style="color: #4c7eb0"
            @click="toDetails(scope.row.id)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 员工数据的分页 -->
    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[3, 5, 10]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
      >
      </el-pagination>
    </div>
    <!-- 模态框开始 -->
    <el-dialog :title="title" :visible.sync="visible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item
          label="用户名:"
          prop="username"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="姓名:"
          prop="realname"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.realname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="手机号:"
          prop="telephone"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.telephone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="密码:"
          prop="password"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.password"
            autocomplete="off"
            type="password"
          ></el-input>
        </el-form-item>

        <el-form-item label="性别:" prop="gender" :label-width="formLabelWidth">
          <el-radio v-model="form.gender" label="female">女</el-radio>
          <el-radio v-model="form.gender" label="male">男</el-radio>
        </el-form-item>
        <el-form-item label="状态:" prop="status" :label-width="formLabelWidth">
          <el-radio v-model="form.status" label="正常"></el-radio>
          <el-radio v-model="form.status" label="禁用"></el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="commitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>

<script>
import request from "../../utils/request";
import {
  addUserWidthRole,
  saveOrUpdate,
  deleteById,
  pageQuery,
} from "@/api/employee";
export default {
  data() {
    return {
      tableData: {},
      form: {},
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        realname: [
          { required: true, message: "请输入真实姓名", trigger: "blur" },
          { min: 1, max: 15, message: "长度为1到15位" },
        ],
        telephone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "长度为11位" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 10, max: 18, message: "密码长度为10到18位" },
        ],
        gender: [{ message: "请选择性别", trigger: "change" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
      title: "",
      visible: false,
      queryParams: {
        page: 1,
        pageSize: 10,
        roleId: 1,
      },
      currentPage: 1,
      formLabelWidth: "80px",
      allId: "",
      // 模糊查询所需数据
      queryData: {
        page: 1,
        pageSize: 10,
        roleId: 1,
        username: "",
        status: "",
      },
    };
  },
  methods: {
    // 去查询
    onSubmit() {
      if (this.queryData.username || this.queryData.status) {
        if (this.queryData.username && !this.queryData.status) {
          let temp = {
            page: this.queryData.page,
            pageSize: this.queryData.pageSize,
            roleId: this.queryData.roleId,
            username: this.queryData.username,
          };
          pageQuery(temp).then((res) => (this.tableData = res.data));
        } else {
          pageQuery(this.queryData).then((res) => (this.tableData = res.data));
        }
      } else {
        pageQuery(this.queryParams).then((res) => (this.tableData = res.data));
      }
    },
    // 去详情
    toDetails(id) {
      this.$router.push({
        path: "/staffDetails/index",
        query: {
          id,
        },
      });
    },
    // 去编辑
    toEdit(row) {
      this.visible = true;
      this.title = "编辑员工";
      this.form = { ...row };
      this.allId = row.id;
    },
    // 去添加
    toAdd() {
      this.visible = true;
      this.title = "添加员工";
      this.form = {};
    },
    // 提交数据
    commitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (!this.allId) {
            let temp = {
              ...this.form,
              roleId: 1,
            };
            addUserWidthRole(temp).then(() => {
              this.visible = false;
              this.$message.success("添加成功");
              this.pageQueryStaff();
            });
          } else {
            saveOrUpdate(this.form).then(() => {
              this.visible = false;
              this.$message.success("更新成功");
              this.allId = "";
              this.pageQueryStaff();
            });
          }
        } else {
          this.$message.info("提交失败");
        }
      });
    },
    // 去删除
    toDelete(staffId) {
      let temp = {
        id: staffId,
      };
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteById(temp).then(() => {
            this.$message.success("删除成功");
            this.pageQueryStaff();
          });
        })
        .catch(() => {
          this.$message.info("取消删除");
        });
    },
    // 删除数据
    //分页查询员工信息
    pageQueryStaff() {
      request
        .get("/baseUser/pageQuery", {
          params: this.queryParams,
        })
        .then((res) => (this.tableData = res.data));
    },

    // 分页方法
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.currentPage = 1;
      this.pageQueryStaff();
    },
    handleCurrentChange(val) {
      this.currentPage = 1;
      this.queryParams.page = val;
      this.pageQueryStaff();
    },
  },
  created() {
    this.pageQueryStaff();
  },
};
</script>

<style  scoped>
.footer {
  position: fixed;
  right: 50px;
}
</style>